/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.dao.StudentsDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.Classes;
import vn.webapp.model.Student;
import vn.webapp.model.Students;

@Service("studentsService")
@Transactional
public class StudentsServiceImpl implements StudentsService{

    @Autowired
    private StudentsDAO studentsDAO;

    public void setStudentsDAO(StudentsDAO studentsDAO) {
        this.studentsDAO = studentsDAO;
    }

/**
 * 
 * @return 
 */
    @Override
    public DataPage<Students> list() {
        DataPage<Students> dataStudents = new DataPage<>();
        dataStudents.setData(studentsDAO.list());
        return dataStudents;
    }
    
    @Override
    public Students viewDetail(int id) {
        return studentsDAO.viewDetail(id);
    }
    
    @Override
    public Students checkExistsCode(String code, int id)
    {
        return studentsDAO.checkExistsCode(code, id);
    }
    
    @Override
    public int removeStudent(int id) {
        return studentsDAO.removeStudent(id);
    }
    
    @Override
    public int updateTheStudent(String code, String name, int classid, int id)
    {
        return studentsDAO.updateTheStudent(code, name, classid, id);
    }
    
    @Override
    public int saveAStudent(String code, String name, int classid)
    {
        Students student = new Students();
        student.setClass_id(classid);
        student.setCode(code);
        student.setName(name);
        return studentsDAO.saveAStudent(student);
    }
    
    @Override
    public DataPage<Students> filter(int page, int pageSize) {
        DataPage<Students> dataStudents = new DataPage<>();
        dataStudents.setPageIndex(page);
        dataStudents.setPageSize(pageSize); 
        dataStudents.setDataCount(studentsDAO.count());
        dataStudents.setPageCount(dataStudents.getDataCount()/pageSize);
        if(dataStudents.getDataCount()%pageSize > 0){
            dataStudents.setPageCount(dataStudents.getPageCount()+1);
        }
        dataStudents.setData(studentsDAO.filter(page, pageSize));
        return dataStudents;
    }
}
